package shiyan4;
public class TestSharedResource {
    static class IncrementThread implements Runnable {
        private SharedResource resource;
        
        public IncrementThread(SharedResource resource) {
            this.resource = resource;
        }
        
        @Override
        public void run() {
            for (int i = 0; i < 5; i++) { // 多次调用 increment，这里示例 5 次，可按需调整
                resource.increment();
            }
        }
    }
    
    static class PrintThread implements Runnable {
        private SharedResource resource;
        
        public PrintThread(SharedResource resource) {
            this.resource = resource;
        }
        
        @Override
        public void run() {
            for (int i = 0; i < 5; i++) { // 多次调用 printValue，和 increment 次数对应
                resource.printValue();
            }
        }
    }
    
    public static void main(String[] args) {
        SharedResource resource = new SharedResource();
        Thread incrementThread = new Thread(new IncrementThread(resource));
        Thread printThread = new Thread(new PrintThread(resource));
        
        incrementThread.start();
        printThread.start();
    }
}